#Copyright 2012 University of Western Sydney
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at

#    http://www.apache.org/licenses/LICENSE-2.0

#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.


import urllib, json, re

solrTemplate = u"""
<doc>
  <field name="id">%(id)s</field>
  <field name="name">%(andsId)s</field>
 
  <field name="kml"><![CDATA[%(kml)s]]></field>  
</doc>


"""
pointTemplate = u"""
<doc>
  <field name="id">%(id)s-%(point)s</field>
  <field name="container">%(id)s</field>  
   <field name="store">%(point)s</field>
</doc>
"""
def fetchData(url,start):
	pageUrl = url + "&start=" + str(start)
	print pageUrl
	f = urllib.urlopen(pageUrl)
	return json.loads(f.read())
	
def getKML(searchUrl, label):
	docCount = 0
	data = fetchData(searchUrl,docCount)
	while docCount < data['response']['numFound']:
	
	    #A degree is about 100km
	    #TODO - put this in config
		searchRadius = 0.1 # ROughly 11 km?
		maxPointsToIndex = 430 # Max "square degrees"
		
		
		toIndex = "<add>";
		for doc in data['response']['docs']:
			andsId = doc['dc_identifier'][0]
			id = doc['id']
			coveragePoints = doc['dc:coverage.vivo:GeographicLocation.1.gn:name']
			
			coverageType =  doc['dc:coverage.vivo:GeographicLocation.1.dc:type'][0]
			print "\n\n************"
			print "Coverage type: " + coverageType
			print "Points " + coveragePoints[0]
			east = -180.0
			north = -90.0
			west = 180.0
			south = 90.0
			try:
				if coverageType == u'iso19139dcmiBox':
					print coveragePoints
					p = re.compile('-?\d+') 
					print p.findall(coveragePoints[0]) 
					points =  p.findall(coveragePoints[0]) 
					north = float(points[0])
					south = float(points[1])
					west = float(points[2])
					east = float(points[3])
				elif coverageType == u'kmlPolyCoords' or coverageType==u'gmlKmlPolyCoords':
			
						points = coveragePoints[0].split(" ");
						for point in points:
							p = point.split(",")
							ew = float(p[0])
							ns = float(p[1])
							if ew >=  east:
								east = ew
							if ew <= west:
								west = ew
							if ns >=  north:
								north = ns
							if ns <= south:
								south = ns

			except:
					"ERROR PROCESSING POINTS"					
				
				
			length = int(north - south)
			width = int(west - east)
			area = abs(length * width)
			print "AREA : %(area)s N %(north)s S %(south)s  W %(west)s  E %(east)s" % locals()	
			if area <= 2:
				point = "%s,%s" % (int(north), int(east))
				toIndex  += pointTemplate % locals()
			elif area < maxPointsToIndex:
				numPoints = 0
				print "N %s S %s W %s E %s" % (int(north), int(south), int(west), int(east))
				for lat in range(int(west),int(east)):
					for lon in range(int(south), int(north)):
						#print "Looping NORTH %s TO SOUTH %s " % (int(north), int(south))
						point = "%s,%s" % (lon, lat)
						toIndex += pointTemplate % locals()
						numPoints += 1
				print "Total points to index: %s" % str(numPoints)
	
			
			kmlFilename = "%s.kml" % id
			print "KML: %s" % kmlFilename
			
			import os.path
			if os.path.exists(kmlFilename):
			    #print "Already got KML"
			    kml = open(kmlFilename).read()
			    kml = kml.decode('utf-8')
			else:
				print "Getting KML"	
				kmlUrl = "https://services.ands.org.au/home/orca/services/getRegistryObjectsKML.php?search=%s" % andsId
				print kmlUrl
				req = urllib.urlopen(kmlUrl)
				kml = req.read()
				kml = kml.decode('utf-8')
				kml = kml.replace("]]>","]]><![CDATA[");
				f = open(kmlFilename, 'w')
				f.write(kml.encode('utf-8'))
				f.close()

			title = doc['dc_title']
			
			toIndex += solrTemplate % locals()
			try:
				toIndex += solrTemplate % locals()
			except:
				print "Warning %s is borked" % kmlFilename
			docCount = docCount + 1
		toIndex += "</add>"
		f = open("collection-%s-%s.xml" % (label,int(docCount)), "w")
		f.write(toIndex.encode('utf-8'))
		f.close()
		toIndex = ""
		data = fetchData(searchUrl, docCount)
getKML("http://115.146.94.66/solr/fascinator/select?q=(dc_coverage:gml)&wt=json", "gml")
getKML("http://115.146.94.66/solr/fascinator/select?q=(dc_coverage:kml)&wt=json", "kml")
getKML("http://115.146.94.66/solr/fascinator/select?q=(dc_coverage:iso19139dcmiBox)&wt=json", "box")
